package com.atgm.mall.gmmailproduct.service.impl;

import com.atgm.common.utils.GmMallUtils;
import com.atgm.mall.gmmailproduct.dao.AttrDao;
import com.atgm.mall.gmmailproduct.entity.AttrEntity;
import com.atgm.mall.gmmailproduct.vo.AttrGroupRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;

import com.atgm.mall.gmmailproduct.dao.AttrAttrgroupRelationDao;
import com.atgm.mall.gmmailproduct.entity.AttrAttrgroupRelationEntity;
import com.atgm.mall.gmmailproduct.service.AttrAttrgroupRelationService;

@Slf4j
@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatch(List<AttrGroupRelationVo> attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> entityList = attrGroupRelationVos.stream().map(attrGroupRelationVo -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrGroupRelationVo, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(entityList);
    }

    @Override
    public List<AttrEntity> queryAttrInfo(Long attrGroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_group_id", attrGroupId);
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = baseMapper.selectList(queryWrapper);

        //获取属性id
        if (GmMallUtils.isEmpty(attrAttrgroupRelationEntities)) {
            return null;
        }

        List<Long> attrIds = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        QueryWrapper<AttrEntity> attrEntityQueryWrapper = new QueryWrapper<>();
        attrEntityQueryWrapper.in("attr_id", attrIds);
        List<AttrEntity> attrEntities = attrDao.selectList(attrEntityQueryWrapper);

        return attrEntities;
    }

    @Override
    public void addAttrAttrGroupRela(List<Long> attrIds, Long attrGroupId) {
        //去除已经存在的关系
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("attr_id");
        queryWrapper.eq("attr_group_id", attrGroupId);
        queryWrapper.in("attr_id", attrIds);
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = baseMapper.selectList(queryWrapper);
        List<Long> selectAttrIdList = new ArrayList<>();
        if (GmMallUtils.isNotEmpty(attrAttrgroupRelationEntities)) {
            selectAttrIdList = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        }
        attrIds.removeAll(selectAttrIdList);
        log.info("attrIds : {}", attrIds);

        //保存数据
        List<AttrAttrgroupRelationEntity> list = new ArrayList<>();
        attrIds.forEach(item -> {
            AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity();
            relation.setAttrId(item);
            relation.setAttrGroupId(attrGroupId);
            relation.setAttrSort(0);
            list.add(relation);
        });
        this.saveBatch(list);
    }
}